﻿using Coldairarrow.Business.PB;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.Entity.TD;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.PB
{
    [Route("/PB/[controller]/[action]")]
    public class PB_ProductionOrderController : BaseApiController
    {
        #region DI

        public PB_ProductionOrderController(IPB_ProductionOrderBusiness pB_ProductionOrderBus)
        {
            _pB_ProductionOrderBus = pB_ProductionOrderBus;
        }

        IPB_ProductionOrderBusiness _pB_ProductionOrderBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<PB_ProductionOrder>> GetDataList(PageInput<ConditionDTO> input)
        {
            return await _pB_ProductionOrderBus.GetDataListAsync(input);
        }

        /// <summary>
        /// 查询结果集-联合查询返回部门名称
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<Entity.PB.PB_ProductionOrderDTO>> GetDataListShowDeptName(PageInput<PB_ProductionOrderConditionDTO> input)
        {
            return await _pB_ProductionOrderBus.GetDataListShowDeptNameAsync(input);
        }

        [HttpPost]
        /// <summary>
        /// sql获取任务单信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<Entity.PB.PB_ProductionOrderDTO>> GetDataPercentageList(PageInput<PB_ProductionOrderConditionDTO> input) {
            var res =await _pB_ProductionOrderBus.GetDataPercentageList(input);
            return res;
        }
        [HttpPost]
        public async Task<PB_ProductionOrder> GetTheData(IdInputDTO input)
        {
            return await _pB_ProductionOrderBus.GetTheDataAsync(input.id);
        }
        /// <summary>
        /// </summary>
        /// <param name="idIn"></param>
        /// <param name="Stats"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AjaxResult> SaveEditTOexamineOrder(string idIn, int Stats) {
            return await _pB_ProductionOrderBus.SaveEditTOexamineOrder(idIn, Stats);
        }

        /// <summary>
        /// 大屏显示当月生产计划数量和生产进度
        /// </summary>
        /// <param name="Month">前第几月</param>
        /// <returns>当月生产计划数量</returns>
        [HttpPost]
        [NoCheckJWT]
        public Coldairarrow.Entity.DTO.ScreenDisplayProgressDTO GetMonthPlanNums(int Month = 0)
        {
            return _pB_ProductionOrderBus.GetMonthPlanNums(Month);
        }
        /// <summary>
        /// 大屏显示当月生产计划重量(吨)和生产进度
        /// </summary>
        /// <param name="Month">前第几月</param>
        /// <returns>当月生产计划重量(吨)</returns>
        [HttpPost]
        [NoCheckJWT]
        public Coldairarrow.Entity.DTO.ScreenDisplayProgressDTO GetMonthPlanWeights(int Month = 0)
        {
            return _pB_ProductionOrderBus.GetMonthPlanWeights(Month);
        }
        /// <summary>
        /// 大屏显示当日总计划重量(吨)
        /// </summary>
        /// <param name="Days">前第几天</param>
        /// <returns>当日总计划重量(吨)</returns>
        [HttpPost]
        [NoCheckJWT]
        public Coldairarrow.Entity.DTO.ScreenDisplayValueDTO GetTodayPlanWeight(int Days=0)
        {
            return _pB_ProductionOrderBus.GetTodayPlanWeight(Days);
        }
        /// <summary>
        /// 大屏显示当日总完成重量(吨)
        /// </summary>
        /// <param name="Days">前第几天</param>
        /// <returns>当日总完成重量(吨)</returns>
        [HttpPost]
        [NoCheckJWT]        
        public Coldairarrow.Entity.DTO.ScreenDisplayValueDTO GetTodayFinishWeight(int Days = 0)
        {
            return _pB_ProductionOrderBus.GetTodayFinishWeight(Days);
        }
        /// <summary>
        /// 大屏显示当日总计划完成率
        /// </summary>
        /// <param name="Days">前第几天</param>
        /// <returns>当日总计划完成率</returns>
        [HttpPost]
        [NoCheckJWT]          
        public Coldairarrow.Entity.DTO.ScreenDisplayValue3DTO GetTodayFinishRate(int Days = 0)
        {
            return _pB_ProductionOrderBus.GetTodayFinishRate(Days);
        }
        /// <summary>
        /// 大屏显示当月指定车间总完成重量(吨)
        /// </summary>
        /// <param name="Month">前第几月</param>
        /// <param name="WorkShop">车间名称</param>
        /// <returns>当月指定车间总完成重量(吨)</returns>
        [HttpPost]
        [NoCheckJWT]
        public Coldairarrow.Entity.DTO.ScreenDisplayValueDTO GetMonthFinishWeightByWorkShop(string WorkShop="",int Month=0)
        {
            return _pB_ProductionOrderBus.GetMonthFinishWeightByWorkShop(Month,WorkShop);
        }
        #endregion
        /// <summary>
        /// 拆分获取任务单名称
        /// </summary>
        /// <param name="WorkShop"></param>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task< List<Entity.DTO.ProductionPlanWorkShop>> ProductionPlanWorkShop(string WorkShop = "")
        {
            return await _pB_ProductionOrderBus.GetProductionPlanWorkShop(WorkShop);
        }
        /// <summary>
        /// 拆分获取进度
        /// </summary>
        /// <param name="WorkShop"></param>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<List<Entity.DTO.GetPassRate>> Gethege(string WorkShop) {
            return await _pB_ProductionOrderBus.Gethege(WorkShop);
        }
        /// <summary>
        /// 拆分获取回货率
        /// </summary>
        /// <param name="WorkShop"></param>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<List<Entity.DTO.OverallQualificationRate>> GerProjectRate(string WorkShop) {
            return await _pB_ProductionOrderBus.GerProjectRate(WorkShop);
        }
      /// <summary>
      /// 拆分获取报工列表
      /// </summary>
      /// <param name="WorkShop"></param>
      /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<List<WorkReportlist>> GetWorkReportlist(string WorkShop) {
            return await _pB_ProductionOrderBus.GetWorkReportlist(WorkShop);
        }
        /// <summary>
        /// 拆分获取报工人数
        /// </summary>
        /// <param name="WorkShop"></param>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<List<Statisticaldata>> GetStatisticaldata(string WorkShop) {
            return await _pB_ProductionOrderBus.GetStatisticaldata(WorkShop);
        }
        #region 提交
        /// <summary>
        /// 根据生产订单id查询明细信息 返回json格式
        /// </summary>
        /// <param name="ParentId">订单id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<string> GetDataLists(string ParentId)
        {
            var res = await _pB_ProductionOrderBus.GetDataLisJson(ParentId);
            return res;
        }
        /// <summary>
        /// 任务单选择清单
        /// </summary>
        /// <param name="ProjectId">设计清单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<PB_ProductionOrder>> GetDataSelectAsync(PageInput<ConditionDTO> input)
        {
            var res = await _pB_ProductionOrderBus.GetDataSelectAsync(input);
            return res;
        }
        /// <summary>
        /// 根据生产订单id查询明细信息 
        /// </summary>
        /// <param name="ParentId">订单id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_ProductionOrderList> GetDataListInfo(string ParentId)
        {
            var res = await _pB_ProductionOrderBus.GetDataListInfo(ParentId);
            return res;
        }

        /// <summary>
        /// 根据生产订单id查询明细信息(供打印)
        /// </summary>
        /// <param name="ParentId">订单id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_ProductionOrderList> GetDataListDetailed(string ParentId)
        {
            var res = await _pB_ProductionOrderBus.GetDataListDetailed(ParentId);
            return res;
        }

        /// <summary>
        /// 保存生产订单及订单明细
        /// </summary>
        /// <param name="data">data</param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveListData(Entity.DTO.PB_ProductionOrderListDTO data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                await _pB_ProductionOrderBus.AddData(data);
            }
            else
            {
                await _pB_ProductionOrderBus.UpdateData(data);
            }
        }
        [HttpPost]
        public async Task ReplaceTask(Entity.DTO.PB_ProductionOrderListDTO data) {
            await _pB_ProductionOrderBus.ReplaceTask(data);
        }

        [HttpPost]
        public async Task SaveData(PB_ProductionOrder data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                await _pB_ProductionOrderBus.AddDataAsync(data);
            }
            else
            {
                await _pB_ProductionOrderBus.UpdateDataAsync(data);
            }
        }

        /// <summary>
        /// 生产任务单重新保存过滤已派工的物料信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpdateTaskDetailList(PB_ProductionOrderListUpdateDTO data) {
            await _pB_ProductionOrderBus.UpdateTaskDetailList(data);
        }

        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _pB_ProductionOrderBus.DeleteDataAsync(ids);
        }

        #endregion

        #region PDA接口
        /// <summary>
        /// PDA生产订单列表接口
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_ProductionOrder>> GetPDADataList(PageInput<ConditionDTO> input)
        {
            var res = await _pB_ProductionOrderBus.GetPDADataListAsync(input);
            return res;
        }

        /// <summary>
        /// 生产领料查询 
        /// </summary>
        /// <param name="cateqrcode">二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_ProductionOrderList> GetPDADataListInfo(string cateqrcode)
        {
            var res = await _pB_ProductionOrderBus.GetPDADataListInfo(cateqrcode);
            return res;
        }



        [HttpPost]
        public async Task PDASaveData(TD_OutStorage data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);
                foreach (var item in data.OutStorDetails)
                {
                    item.OutStorId = data.Id;
                    item.TotalAmt = item.Price * item.LocalNum;
                }
                await _pB_ProductionOrderBus.AddPDADataAsync(data);
            }

        }

        /// <summary>
        /// 生产任务单根据设计清单选择物料（明细汇总）
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<DesignMaterialDTO>> GetDesignMaterialList(PageInput<DesignMaterialConditionDTO> input)
        {
            return await _pB_ProductionOrderBus.GetDesignMaterialListAsync(input);
        }


        /// <summary>
        /// 生产任务单根据设计清单选择物料（明细）
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<DesignMaterialDTO>> GetDesignMaterialDetailList(PageInput<DesignMaterialConditionDTO> input)
        {
            return await _pB_ProductionOrderBus.GetDesignMaterialDetailListAsync(input);
        }

 /// <summary>
        /// 任务单明细变更替换成品
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        //[HttpPost]
        //public async Task ChangeTaskDetail(ProductionOrderDetailReplacement data) {
        //    await _pB_ProductionOrderBus.ChangeTaskDetail(data);
        //}

        #endregion

        #region   组件生产任务单

        /// <summary>
        /// 获取需要组件的BOM物料信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<BomMaterialDTO>> GetBomMaterialList(PageInput<OrderBomMaterialConditionDTO> input)
        {
            return await _pB_ProductionOrderBus.GetBomMaterialListAsync(input);
        }

        #endregion

        #region 生产任务单明细派工详情

        /// <summary>
        /// 获取生产任务单明细的派工信息
        /// </summary>
        /// <param name="orderdetailId">任务单明细Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<OrderDetailDispatchDetailDTO>> GetOrderDetailDispatchDetailList(string orderdetailId)
        {
            return await _pB_ProductionOrderBus.GetOrderDetailDispatchDetailListAsync(orderdetailId);
        }

        [HttpPost]
        public async Task ChangeTaskDetail(ChangeTaskDetailDto data) {
            await _pB_ProductionOrderBus.ChangeTaskDetail(data);
        }

        /// <summary>
        /// 打印流转单
        /// </summary>
        /// <param name="DispatchDtailId"></param>
        /// <param name="SubId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<CirculationDetails>> PrintCirculationDetails(string DispatchDtailId, string SubId)
        {
            var res= await _pB_ProductionOrderBus.PrintCirculationDetails(DispatchDtailId, SubId);
            return res;
        }
        #endregion

    }
}